package it.wyx.springbootkafkaproducer.service.bsj;

import com.alibaba.fastjson.JSONArray;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;
import javax.annotation.Resource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.stereotype.Service;

/**
 * @author wyx
 * @version V 1
 * @Date 2022/12/6
 */
@Service
public class BsjProducerService {

  @Resource
  private KafkaTemplate<String, String> bsjKafkaTemplate;

  public void pushMiniAppPkgCreatedResult(String topic,BsjMiniAppPkgCreatedResult param) {
    try {
      Map<String, Object> params = new ConcurrentHashMap<>();
      params.put("uuid", UUID.randomUUID().toString());
      params.put("orderId", param.getOrderId());
      params.put("externalOrderId", param.getExternalOrderId());
      params.put("paymentNo", param.getPaymentNo());
      params.put("packageStatus", param.getPackageStatus());
      params.put("packageId", param.getPackageId());
      params.put("validTo", param.getValidTo());
      params.put("packageActiveTime", param.getPackageActiveTime()); /* 消息发送时间*/
      params.put("msgCreateTime", LocalDateTime.now().toInstant(ZoneOffset.of("Z")).toEpochMilli());
      bsjKafkaTemplate.send(topic, JSONArray.toJSONString(params));

      System.out.println("Kafka message of package send to OMP : " + params);
    } catch (Exception e) {
      System.out.println("pushMiniAppPkgCreatedResult-error" + e);
    }
  }
}
